package research.ui.editors;

import java.util.List;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.ui.forms.widgets.TableWrapData;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.DefaultXYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.experimental.chart.swt.ChartComposite;

import research.domain.Point;
import research.domain.Sample;
import research.domain.SampleStabilizer;
import research.domain.Stabilizer;
import research.entity.Entity;
import research.entity.EntityType;
import research.model.EntityListContentProvider;
import research.model.ListHierarchy;
import research.model.SampleStabilizerPointHierarchy;
import research.persistence.PersistenceManager;
import research.ui.editors.input.EntityEditorInput;

public class SampleStabilizerEditor extends FormEntityEditor {
	public static String ID = "research.ui.editors.SampleStabilizerEditor";
	private ComboViewer stabilizer;
	private ComboViewer sample;
	private Text name;
	private XYSeries data = new XYSeries("data");
	
	private SampleStabilizer getEntity() {
		return (SampleStabilizer) entity;
	}

	@Override
	protected boolean checkInputEntityType(EntityEditorInput input) {
		return input.getEntity().getType().equals(EntityType.SampleStabilizer);
	}

	@Override
	protected void fill() {
		this.name.setText(this.getEntity().getName());

		setComboEntity(sample, this.getEntity().getSample());
		setComboEntity(stabilizer, this.getEntity().getStabilizer());
	}
	
	@Override
	protected void initialize() {
		if (!isNewEntity()) {
			List<Entity> s = PersistenceManager.getInstance().getDataSource().getChildren(getEntity(), new SampleStabilizerPointHierarchy()); 
			for (Entity point : s) {
				getData().add(((Point) point).getX(), ((Point) point).getY());
			}
		}
		
	}

	@Override
	protected boolean readAndValidate() {
		this.getEntity().setName(this.name.getText());
		
		Sample cal = (Sample) getComboEntity(sample);
		Stabilizer stab = (Stabilizer) getComboEntity(stabilizer);
		
		if (stab == null) {
			MessageDialog.openWarning(getSite().getShell(), "Error",
					"Выберите испытание стабилизатора");
			return false;
		}

		this.getEntity().setSample(cal);
		this.getEntity().setStabilizer(stab);

		return validateText(name);
	}

	@Override
	protected void renderForm() {
		TableWrapData td;
		Label label;
		Section section;
		Composite composite;
		GridData gd;
		
		/*
		 * Sample
		 */
		section = toolkit.createSection(form.getBody(), Section.TITLE_BAR);
		td = new TableWrapData(TableWrapData.FILL);
		section.setLayoutData(td);
		section.setText("Образец");
		composite = toolkit.createComposite(section);
		composite.setLayout(new GridLayout(2, false));
		section.setClient(composite);
		gd = new GridData();
		label = toolkit.createLabel(composite, "Выберите образец:");
		label.setLayoutData(gd);
		gd = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL);
		sample = new ComboViewer(composite, SWT.DROP_DOWN
				| SWT.READ_ONLY);
		sample.setContentProvider(new EntityListContentProvider(new ListHierarchy(EntityType.Sample), true));
		sample.setLabelProvider(new LabelProvider());
		sample.setInput(PersistenceManager.getInstance());
		gd.widthHint = 100;
		sample.getCombo().setLayoutData(gd);
		sample.getCombo().addModifyListener(getTextModifyListener());
		
		/*
		 * Stabilizer
		 */
		section = toolkit.createSection(form.getBody(), Section.TITLE_BAR);
		td = new TableWrapData(TableWrapData.FILL);
		section.setLayoutData(td);
		section.setText("Исследование стабилизатора");
		composite = toolkit.createComposite(section);
		composite.setLayout(new GridLayout(2, false));
		section.setClient(composite);
		gd = new GridData();
		label = toolkit.createLabel(composite, "Выберите исследование*:");
		label.setLayoutData(gd);
		gd = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL);
		stabilizer = new ComboViewer(composite, SWT.DROP_DOWN
				| SWT.READ_ONLY);
		stabilizer.setContentProvider(new EntityListContentProvider(new ListHierarchy(EntityType.Stabilizer), false));
		stabilizer.setLabelProvider(new LabelProvider());
		stabilizer.setInput(PersistenceManager.getInstance());
		gd.widthHint = 100;
		stabilizer.getCombo().setLayoutData(gd);
		stabilizer.getCombo().addModifyListener(getTextModifyListener());

		/*
		 * Name
		 */
		section = toolkit.createSection(form.getBody(), Section.TITLE_BAR);
		td = new TableWrapData(TableWrapData.FILL_GRAB);
		section.setLayoutData(td);
		section.setText("Название*");
		composite = toolkit.createComposite(section);
		composite.setLayout(new GridLayout(1, false));
		section.setClient(composite);
		gd = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL);
		name = toolkit.createText(composite, "",
				SWT.BORDER);
		gd.widthHint = 100;
		name.setLayoutData(gd);
		name.addModifyListener(getNameModifyListener());

		/*
		 * Chart
		 */
		section = toolkit.createSection(form.getBody(), Section.TITLE_BAR
				| Section.EXPANDED | Section.TITLE_BAR | Section.TWISTIE);
		td = new TableWrapData(TableWrapData.FILL_GRAB);
		td.maxHeight = 300;
		section.setLayoutData(td);
		section.setText("График");
		composite = toolkit.createComposite(section);
		composite.setLayout(new GridLayout(1, false));
		section.setClient(composite);
		gd = new GridData(GridData.FILL_BOTH | GridData.GRAB_VERTICAL);
		gd.heightHint = 300;
		DefaultXYDataset dataset = new DefaultXYDataset();
		dataset.addSeries("data", getData().toArray());
		JFreeChart chart = ChartFactory.createXYLineChart("", "",
				"", dataset, PlotOrientation.VERTICAL, false, false,
				false);
		chart.setAntiAlias(false);
		ChartComposite comp = new ChartComposite(composite, SWT.NONE, chart,
				true);
		comp.setLayoutData(gd);

	}
	
	public XYSeries getData() {
		return data;
	}

}
